﻿package com.jxl.dnd
{
	import com.jxl.charactersheet.collections.InventoryCollection;
	import com.jxl.charactersheet.events.InventoryChangeEvent;
	import com.jxl.charactersheet.vo.inventory.WearableItem;
	import com.jxl.dnd.abilities.AbilityScores;
	import com.jxl.dnd.combat.ArmorClass;
	import com.jxl.dnd.combat.AttackBonus;
	import com.jxl.dnd.combat.HitPoints;
	import com.jxl.dnd.combat.SavingThrows;
	import com.jxl.dnd.events.DnDEvent;
	import com.jxl.dnd.races.Race;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	
	public class Character extends EventDispatcher
	{
		[Bindable("abilityScoresChanged")]
		public function get abilityScores():AbilityScores { return __abilityScores; }
		public function set abilityScores(val:AbilityScores):void
		{
			var old:AbilityScores = __abilityScores;
			if(old != null)
			{
				old.removeEventListener(DnDEvent.ABILITY_SCORE_CHANGED, onAbilityScoreChanged);
			}
			__abilityScores = val;
			__abilityScores.addEventListener(DnDEvent.ABILITY_SCORE_CHANGED, onAbilityScoreChanged);
			if(__attackBonus != null)
			{
				__attackBonus.abilityScores = __abilityScores;
			}
			dispatchEvent(new DnDEvent(DnDEvent.ABILITY_SCORES_CHANGED));
		}
		
		[Bindable]
		public var hitPoints:HitPoints;
		
		[Bindable]
		public var armorClass:ArmorClass;
		
		[Bindable("attackBonusChanged")]
		public function get attackBonus():AttackBonus { return __attackBonus; }
		public function set attackBonus(val:AttackBonus):void
		{
			__attackBonus = val;
			if(__abilityScores != null)
			{
				__attackBonus.abilityScores = __abilityScores;
			}
			dispatchEvent(new Event("attackBonusChanged"));
		}
		
		[Bindable]
		[ArrayElementType("com.jxl.charactersheet.vo.inventory.WearableItem")]
		public function get equipment():InventoryCollection { return __equipment; }
		public function set equipment(val:InventoryCollection):void
		{
			if(__equipment != null)
			{
				__equipment.removeEventListener(InventoryChangeEvent.INVENTORY_CHANGED, onInventoryChanged);
				__equipment = null;
			}
			
			if(val != null)
			{
				__equipment = val;
				__equipment.addEventListener(InventoryChangeEvent.INVENTORY_CHANGED, onInventoryChanged);
			}
			
			onInventoryChanged(new InventoryChangeEvent(InventoryChangeEvent.INVENTORY_CHANGED));
		}
		
		[Bindable]
		[ArrayElementType("com.jxl.dnd.feats.Feat")]
		public var feats:ArrayCollection;
		
		protected var race:Race;
		protected var characterClass:Class;
		protected var savingThrows:SavingThrows;
		protected var skills_array:Array;
		
		protected var descpription_str:String;
		
		protected var __attackBonus:AttackBonus;
		protected var __abilityScores:AbilityScores;
		protected var __equipment:InventoryCollection;
		
		
		
		public var name:String;
		public var age:String;
		public var gender:String;
		public var alignment:String;
		public var appearence:String;
		
		public function Character():void
		{
		}
		
		protected function onAbilityScoreChanged(event:DnDEvent):void
		{
			switch(event.abilityScoreType)
			{
				case AbilityScores.STRENGTH:
				
				case AbilityScores.DEXTERITY:
				
				case AbilityScores.CONSTITUTION:
				
				case AbilityScores.INTELLIGENCE:
				
				case AbilityScores.WISDOM:
				
				case AbilityScores.CHARISMA:
			}
		}
		
		public function toXML():XML
		{
			var charXML:XML = <character />;
			charXML.appendChild(abilityScores.toXML());
			charXML.appendChild(armorClass.toXML());
			charXML.appendChild(hitPoints.toXML());
			charXML.appendChild(attackBonus.toXML());
			charXML.appendChild(equipment.toXML());
			/*
			var featsXML:XML = <feats />;
			var featsLen:int = feats.length;
			var feat:Feat;
			for(var featIndex:uint = 0; featIndex<featsLen; featIndex++)
			{
				feat = feats.getItemAt(featIndex) as Feat;
				featsXML.appendChild(feat.toXML());
			}
			charXML.appendChild(featsXML);
			*/
			
			return charXML;
		}
		
		public function buildFromXML(xml:XML):void
		{
			var abilityScores:AbilityScores 			= new AbilityScores();
			abilityScores.buildFromXML(xml);
			this.abilityScores = abilityScores;
			
			var armorClass:ArmorClass					= new ArmorClass();
			armorClass.buildFromXML(xml);
			this.armorClass								= armorClass;
			
			var hitPoints:HitPoints						= new HitPoints();
			hitPoints.buildFromXML(xml);
			this.hitPoints								= hitPoints;
			
			var attackBonus:AttackBonus					= new AttackBonus();
			attackBonus.buildFromXML(xml);
			this.attackBonus							= attackBonus;
			
			var equipment:InventoryCollection			= new InventoryCollection();
			var equipmentXML:XML = xml..equipment[0] as XML;
			equipment.buildFromXML(equipmentXML);
			this.equipment								= equipment;
			
			var feats:Array								= [];
			
		}
		
		protected function onInventoryChanged(event:InventoryChangeEvent):void
		{
			var len:int = __equipment.length;
			var item:WearableItem;
			for(var i:uint = 0; i<len; i++)
			{
				item = __equipment.getItemAt(i) as WearableItem;
				
			}
		}
	}
}